/*
 =============================================================================
 FILE...............: ConnectionFactory.java
 COMMENTS...........: Connection factory class implemented using the 
 					  Factory design pattern.
 =============================================================================
 AUTHOR(S)..........: Everton Ranielly de Sousa Cavalcante
 					  Department of Informatics and Applied Mathematics
 					  Federal University of Rio Grande do Norte
 LAST MODIFICATION..: September 26th, 2012
 ============================================================================= 					  
*/


/* ========================================================================= */
/* PACKAGES			                                                         */
/* ------------------------------------------------------------------------- */
package dao;


/* ========================================================================= */
/* IMPORTS			                                                         */
/* ------------------------------------------------------------------------- */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/* ========================================================================= */
/* CLASS ConnectionFactory                                                   */
/* ------------------------------------------------------------------------- */
/**
 * Connection factory class implemented by using the Factory design pattern
 * @author Everton Cavalcante
 */
public class ConnectionFactory 
{
	// MySQL driver
	private static final String MySQLDriver = "com.mysql.jdbc.Driver";
	
	
	/**
	 * Creates a new connection using the provided database URL, login and password 
	 * given by a properties file
	 * @param propertiesFile Properties file
	 * @return New connection
	 */
	public static Connection connection(String propertiesFile)
	{
		try 
		{
			// Reads the properties file
			Properties properties = new Properties();
			FileInputStream input = new FileInputStream(propertiesFile);
			properties.load(input);
			input.close();
			
			// Sets the parameters
			String url = properties.getProperty("jdbc.url");
			String user = properties.getProperty("jdbc.user");
			String password = properties.getProperty("jdbc.pwd");
			
			// Loads the MySQL driver and return a MySQL connection with the given parameters
			Class.forName(MySQLDriver);
			return DriverManager.getConnection(url, user, password);
		} 
		catch (ClassNotFoundException ex) 
		{ 
			System.out.println("Error:\nDriver JDBC-ODBC not found");
			System.exit(1);
		}
		catch (SQLException ex) 
		{
			System.out.println("Error during connection:\n" + ex.getMessage());
			System.exit(1);
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
			System.exit(1);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			System.exit(1);
		}
		
		return null;
	}
}
